package cetcbigdata.da.personal_service.shanghai;

import cetcbigdata.da.base.Base;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;
import java.util.ArrayList;
import java.util.List;


public class RegionSpider extends Base {
    private static String baseRegion = "上海市";

    private static String baseModule = "个人服务";

    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "ss", "ss");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        try {
            for (JSONObject r : getRegion()) {
                for (JSONObject m : getModule()) {
                    for (JSONObject i : getItemType()) {
                        this.parseListPage(recordSender, fieldsList, r, m, i);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private JSONObject parseListPage(RecordSender recordSender, List<Object> fieldsList, JSONObject regionItem, JSONObject moduleItem, JSONObject itemType) {
        //获取区名和类型名称
        String region = baseRegion + "-" + regionItem.getString("name");
        String module = baseModule + "-" + moduleItem.getString("name") + "-" + itemType.getString("name");

        //获取类型编码
        String category = moduleItem.getString("category");
        String regCode = regionItem.getString("regCode");
        String itemsType = itemType.getString("itemType");
        try {
            // 当前页
            int currentPage = 1;
            while (true){
                String listUrl = "https://zwdt.sh.gov.cn/govPortals/person.do";
                JSONObject listHeader = new JSONObject();
                listHeader.put("Referer", "https://zwdt.sh.gov.cn/govPortals/naturalPerson/group");
                listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36");
                JSONObject listParams = new JSONObject();
                listParams.put("pageNumber", currentPage);
                listParams.put("pageSize", 50);
                listParams.put("itemType", itemsType);
                listParams.put("orgCode",  "");
                listParams.put("stRegion",  regCode);
                listParams.put("category", category);
                listParams.put("gfType", "");
                listParams.put("sort", "");
                listParams.put("feature", "");
                listParams.put("stKeyword", "");
                listParams.put("stNet", "");

                String listResponse = postFormReq(listUrl, listHeader, listParams);
                listResponse = new String(listResponse.getBytes("iso-8859-1"), "utf-8");
                JSONObject listResponseObj = JSONObject.parseObject(listResponse);
                JSONObject itemList = listResponseObj.getJSONObject("itemList");


                //总页数
                int count = listResponseObj.getInteger("count");
                int pageSize = listResponseObj.getInteger("pageSize");
                int pages = (count / pageSize) + 1;

                //每页每项遍历
                for (String key : itemList.keySet()){
                    JSONObject contentParent =new JSONObject();

                    //得到一级标题
                    String title = key;
                    List<JSONObject> contentList = new ArrayList<>();
                    String detailUrl = null;
                    String contentResponse = null;
                    for(Object o : itemList.getJSONArray(key)){
                        JSONObject item = (JSONObject) o;
                        String stItemCode = item.getString("stId");

                        //得到二级标题
                        String stItemName = item.getString("stSubitemName");

                        //获取请求详情页的参数
                        String url = "https://zwdt.sh.gov.cn/govPortals/item/getFwznWithByItemId.do";
                        JSONObject header = new JSONObject();
                        JSONObject params = new JSONObject();
                        params.put("stId", stItemCode);
                        String stResponse = postFormReq(url, header, params);
                        JSONArray stList = JSONArray.parseArray(stResponse);
                        for (Object s : stList) {
                            JSONObject ss = (JSONObject) s;
                            String bsfwCode = ss.getJSONObject("item").getString("ST_ID");
                            detailUrl = String.format("https://zwdt.sh.gov.cn/govPortals/bsfw/item/%s", bsfwCode);
                            JSONObject detailHeader = new JSONObject();
                            JSONObject detailParams = new JSONObject();

                            //请求详情页
                            JSONObject contentChild =new JSONObject();
                            contentResponse = getReq(detailUrl, detailHeader, detailParams);

                            String ST_TRANSACT_NAME = isNotBlankOrNull(ss.getJSONObject("item").getString("ST_TRANSACT_NAME")) ?
                                    "===" +  ss.getJSONObject("item").getString("ST_TRANSACT_NAME") : "";
                            contentChild.put(stItemName + ST_TRANSACT_NAME, getDetailPage(contentResponse));
                            contentList.add(contentChild);
                        }
                    }
                    contentParent.put("名称", title);
                    contentParent.put("选项", contentList);

                    processResult(
                            listUrl, listResponse, detailUrl, contentResponse, contentParent, region, module
                    );
                }

                if (currentPage >= pages){
                    break;
                }else {
                    //更新当前页
                    currentPage ++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }

    private static JSONObject getDetailPage(String html){
        JXDocument jxDocument = new JXDocument(html);
        JSONObject content = new JSONObject();
        content.put("前言信息", ShUtil.geExtraInfo(jxDocument));
        content.put("基本信息", ShUtil.getBaseInfo(jxDocument));
        content.put("受理条件", ShUtil.getAcceptanceConditions(jxDocument));
        content.put("申请材料", ShUtil.getApplicationMaterialsInfo(jxDocument));
        content.put("办理流程", ShUtil.getProcess(jxDocument));
        content.put("办理地点/时间", ShUtil.getPlaceOrTime(jxDocument));
        content.put("常见问题", ShUtil.getProblemInfo(jxDocument));
        content.put("服务依据", ShUtil.getServiceBasis(jxDocument));
        content.put("服务收费", ShUtil.getServiceCharge(jxDocument));
        return content;
    }

    private static List<JSONObject> getRegion(){
        List<JSONObject> out = new ArrayList<>();
        String region = "[" +
                "{\"name\":\"嘉定区\",\"regCode\":\"SH00JD\"}, " +
                "{\"name\":\"静安区\",\"regCode\":\"SH00JA\"}, " +
                "{\"name\":\"杨浦区\",\"regCode\":\"SH00YP\"}, " +
                "{\"name\":\"松江区\",\"regCode\":\"SH00SJ\"}, " +
                "{\"name\":\"浦东新区\",\"regCode\":\"SH00PD\"}, " +
                "{\"name\":\"奉贤区\",\"regCode\":\"SH00FX\"}, " +
                "{\"name\":\"宝山区\",\"regCode\":\"SH00BS\"}, " +
                "{\"name\":\"徐汇区\",\"regCode\":\"SH00XH\"}, " +
                "{\"name\":\"长宁区\",\"regCode\":\"SH00CN\"}, " +
                "{\"name\":\"崇明区\",\"regCode\":\"SH00CM\"}, " +
                "{\"name\":\"金山区\",\"regCode\":\"SH00JS\"}, " +
                "{\"name\":\"黄浦区\",\"regCode\":\"SH00HP\"}, " +
                "{\"name\":\"普陀区\",\"regCode\":\"SH00PT\"}, " +
                "{\"name\":\"虹口区\",\"regCode\":\"SH00HK\"}, " +
                "{\"name\":\"青浦区\",\"regCode\":\"SH00QP\"}, " +
                "{\"name\":\"闵行区\",\"regCode\":\"SH00MH\"}" +
                "]";
        JSONArray array = JSONArray.parseArray(region);
        for (Object o : array) {
            JSONObject oo = (JSONObject) o;
            out.add(oo);
        }
        return out;
    }

    private static List<JSONObject> getModule(){
        List<JSONObject> out = new ArrayList<>();

        String url = "https://zwdt.sh.gov.cn/govPortals/item/getCategoryByType.do";
        JSONObject header = new JSONObject();
        JSONObject params = new JSONObject();

        //请求类型地址
        String response = postFormReq(url, header, params);
        JSONObject responseObj = JSONObject.parseObject(response);
        JSONArray gr = responseObj.getJSONArray("gr");
        for (Object o : gr) {
            JSONObject items = new JSONObject();
            JSONObject oo = (JSONObject) o;
            String name = oo.getString("ST_DIC_NAME");
            String code = oo.getString("ST_DIC_DESC");
            items.put("name", name);
            items.put("category", code);
            out.add(items);
        }
        return out;
    }

    private static List<JSONObject> getItemType(){
        List<JSONObject> out = new ArrayList<>();
        String itemUrl = "https://zwdt.sh.gov.cn/govPortals/region/SH00SH";
        JSONObject itemHeader = new JSONObject();
        JSONObject itemParams = new JSONObject();

        String itemResponse = getReq(itemUrl, itemHeader, itemParams);
        JXDocument document = new JXDocument(itemResponse);
        List<Object> rs = document.sel("//div[@class='form-group']//select[@id='itemTypeSelect']//option");
        for (Object r : rs) {
            JSONObject items = new JSONObject();
            String name = ((Element) r).text();
            String itemType = ((Element) r).attr("value");
            items.put("name", name);
            items.put("itemType", itemType);
            out.add(items);
        }
        return out;
    }

    public static void main(String[] args) {
        RegionSpider spider = new RegionSpider();
        spider.getData(null, new ArrayList<>());
    }
}
